#include "Search_Operation.h"


Search_Operation::Search_Operation(void):size_limit(0),time_limit(0)
{
	search_base = false;
}


Search_Operation::~Search_Operation(void)
{
}





Search_Operation::Search_Operation(char *dn,int auth_type,char **ldapOptions,int nr_opt,char *ldap_URI, bool search_base , vector<Atribut<char*>> filtru)
	:LDAP_Operation(dn,auth_type,ldap_URI,ldapOptions,nr_opt)
{
	this->size_limit = 0;
	this->time_limit = 0;
	this->search_base = search_base;

	this->search_filter = filtru;
}


Search_Operation::Search_Operation(char *dn,int auth_type,char **ldapOptions,int nr_opt,char *ldap_URI,bool search_base)
	:LDAP_Operation(dn,auth_type,ldap_URI,ldapOptions,nr_opt)
{
	
	this->size_limit = 0;
	this->time_limit = 0;
	this->search_base = search_base;

	

}



LDAP_Operation* Search_Operation::operate()
{
		//formatarea result pentru entry
	result=new char[1000];
	strcpy(result,"ldapsearch ");

	switch(this->auth_type)
	{
	case 0: strcat(result,"-x ");
		break;
	case 1: strcat(result," ");
		break;
	default: printf("\nExceptie:");
		//exceptie;
	}

	for(int i=0;i<nr_opt;i++)
	{
		strcat(result,option[i]);
		strcat(result," ");
	}


	if(strlen(Ldap_URI))
	{
		strcat(result,"-H ");
		strcat(result,Ldap_URI);
		strcat(result," ");
	}

	if(this->search_base)
		strcat(result,"-b ");

	//adaugarea dn-ului

	if(strlen(this->DN))
		strcat(result,this->DN);
	strcat(result," ");

	// adaugarea atributelor-filtrelor

	
	for(int i=0;i<search_filter.size();i++)
	{
		strcat(result,search_filter[i].daNume());
		strcat(result," ");
	}

	strcat(result,"*");

	LDAP_Operation *op=new Search_Operation((*this));
	//!!!!!!!! SMART POINTERI PT A LUCRA CU CEEA CE RETURNEZI
	return op;
}

